﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;

namespace SharpGeo
{
    /// <summary>
    /// A multi-purpose coordinate that can support an arbitrary number of axes
    /// </summary>
    public class Coordinate :
       IEquatable<ICoordinate>,
       IGeographicCoordinate,
       IGeocentricCoordinate,
       IScreenCoordinate,
       ICoordinate
    {
        #region Constructors

        /// <summary>
        /// Constructs a new Coordinate from doubles
        /// </summary>
        /// <param name="ordinates">The doubles that represent the coordinate</param>
        public Coordinate(params double[] ordinates)
        {
            if (ordinates == null)
                throw new ArgumentNullException("ordinates",
                    Properties.Resources.CoordinatesOrdinatesNullException);

            _ordinates = new double[ordinates.Length];

            for (int i = 0; i < ordinates.Length; i++)
            {
                _ordinates[i] = ordinates[i];
            }
        }

        #endregion Constructors

        #region Fields

        private double[] _ordinates;

        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets the Latitude value
        /// </summary>
        public double Latitude
        {
            get { return this[1]; }
        }

        /// <summary>
        /// Gets the Longitude value
        /// </summary>
        public double Longitude
        {
            get { return this[0]; }
        }

        /// <summary>
        /// Gets the number of Axes
        /// </summary>
        public int NumberOfAxes
        {
            get { return _ordinates.Length; }
        }

        /// <summary>
        /// Gets the X value
        /// </summary>
        public double X
        {
            get { return this[0]; }
        }

        /// <summary>
        /// Gets the Y value
        /// </summary>
        public double Y
        {
            get { return this[1]; }
        }

        /// <summary>
        /// Gets the Z value
        /// </summary>
        public double Z
        {
            get { return this[2]; }
        }

        /// <summary>
        /// Gets the oordinate at the specified axis
        /// </summary>
        /// <param name="axis">The axis of the oordinate to retrieve</param>
        /// <returns>Returns the oordiante at the specified axis, if the axis not between 0 and 1, double.NaN is returned.</returns>
        public double this[int axis]
        {
            get { return axis < _ordinates.Length ? _ordinates[axis] : double.NaN; }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Tests two Coordinates for equality
        /// </summary>
        /// <param name="obj">The other Coordinate to compare</param>
        /// <returns>True if the coordinates are equal</returns>
        public override bool Equals(object obj)
        {
            var coord = obj as ICoordinate;

            if (coord == null)
                return false;

            return Equals(coord);
        }

        /// <summary>
        /// Tests two Coordinates for equality
        /// </summary>
        /// <param name="other">The other Coordinate</param>
        /// <returns>True if the coordinates are equal</returns>
        public bool Equals(ICoordinate other)
        {
            if (other == null)
                return false;

            if (((ICoordinate)this).NumberOfAxes != other.NumberOfAxes)
                return false;

            for (int i = 0; i < ((ICoordinate)this).NumberOfAxes; i++)
            {
                if (this[i] != other[i])
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Computes the hashcode of the coordinate
        /// </summary>
        /// <returns>The computed hashcode</returns>
        public override int GetHashCode()
        {
            if (_ordinates.Length == 2)
            {
                unchecked
                {
                    long l = BitConverter.DoubleToInt64Bits(_ordinates[1]);
                    l *= l * 31 ^ BitConverter.DoubleToInt64Bits(_ordinates[0]);
                    return (int)((l >> 32) ^ l);
                }
            }

            if (_ordinates.Length == 3)
            {
                unchecked
                {
                    long l = BitConverter.DoubleToInt64Bits(_ordinates[2]);
                    l *= l * 31 ^ BitConverter.DoubleToInt64Bits(_ordinates[1]);
                    l *= l * 37 ^ BitConverter.DoubleToInt64Bits(_ordinates[0]);

                    return (int)unchecked((l >> 32) ^ l);
                }
            }

            unchecked
            {
                int hashCode = _ordinates[0].GetHashCode();
                for (int i = 1; i < _ordinates.Length; i++)
                {
                    hashCode ^= _ordinates[i].GetHashCode();
                }
                return hashCode;
            }
        }

        /// <summary>
        /// Converts this Coordinate to a string
        /// </summary>
        /// <returns>A string representing the Coordinate</returns>
        public override string ToString()
        {
            return string.Format(
                System.Globalization.CultureInfo.InvariantCulture,
                "({0})",
                string.Join<double>(",", _ordinates));
        }

        #endregion Methods
    }
}